package com.A21MobileSales;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.A21.Adapter.CustomerSwipeListAdapter;
import com.A21.Object.MSTCustomer;
import com.A21MobileSales.DataBase.DatabaseHelper;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.SwipeListView;

public class CustomerMaster extends Activity {

	SwipeListView lvCustomer;
	EditText edtSearchCustomer;

	DatabaseHelper ObjDBHelper;

	ArrayList<MSTCustomer> lstCustomers;
	Button btnDelSearch;
	CustomerSwipeListAdapter customerSwipeListAdapter;

	Button btn_Home;
	ImageButton btn_back;
	String TrnType;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customers);
		try {
			lvCustomer = (SwipeListView) findViewById(R.id.CS_ListviewCustomer);
			btn_Home = (Button) findViewById(R.id.btn_home);
			btn_Home.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent _intent = new Intent();
					_intent.setClass(CustomerMaster.this, HomeScreen.class);
					startActivity(_intent);
				}
			});

			btn_back = (ImageButton) findViewById(R.id.btn_back);
			btn_back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					GoBack();
				}
			});

			ObjDBHelper = new DatabaseHelper(this);
			btnDelSearch = (Button) findViewById(R.id.btn_del_search);
			btnDelSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edtSearchCustomer.setText("");
					// customerSwipeListAdapter.notifyDataSetChanged();
					// lvCustomer.setAdapter(customerSwipeListAdapter);
				}
			});

			lvCustomer
					.setSwipeListViewListener(new BaseSwipeListViewListener() {
						@Override
						public void onOpened(int position, boolean toRight) {
						}

						@Override
						public void onClosed(int position, boolean fromRight) {
						}

						@Override
						public void onListChanged() {
						}

						@Override
						public void onMove(int position, float x) {
						}

						@Override
						public void onStartOpen(int position, int action,
								boolean right) {
							// Log.d("swipe",
							// String.format("onStartOpen %d - action %d",
							// position, action));
						}

						@Override
						public void onStartClose(int position, boolean right) {
							// Log.d("swipe", String.format("onStartClose %d",
							// position));
						}

						@Override
						public void onClickFrontView(int position) {
							// Log.d("swipe",
							// String.format("onClickFrontView %d", position));
							// lvCustomer.openAnimate(position); //when you
							// touch front view it will open

						}

						@Override
						public void onClickBackView(int position) {
							// Log.d("swipe",
							// String.format("onClickBackView %d", position));
							// lvCustomer.closeAnimate(position);//when you
							// touch back view it will close
						}

						@Override
						public void onDismiss(int[] reverseSortedPositions) {

						}

					});

			// lvCustomer.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH); // there
			// are five swiping modes
			lvCustomer.setSwipeMode(SwipeListView.SWIPE_MODE_RIGHT); // there
																		// are
																		// five
																		// swiping
																		// modes
			lvCustomer.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_DISMISS); // there
																				// are
																				// four
																				// swipe
																				// actions
			lvCustomer.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
			lvCustomer.setOffsetLeft(convertDpToPixel(0f)); // left side offset
			float fRight = getResources().getDimension(R.dimen.SwipeRight);
			lvCustomer.setOffsetRight(convertDpToPixel(fRight)); // right side
																	// offset
			lvCustomer.setAnimationTime(500); // Animation time
			// lvCustomer.setSwipeOpenOnLongPress(true); // enable or disable
			// SwipeOpenOnLongPress
			edtSearchCustomer = (EditText) findViewById(R.id.edtSearchCustomer);
			edtSearchCustomer.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					String text = s.toString().toLowerCase(Locale.getDefault());
					customerSwipeListAdapter.filter(text);
				}
			});

			new CustomerTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void GoBack() {
		Bundle b = new Bundle();
		Intent _intent = new Intent();
		_intent.putExtras(b);
		_intent.setClass(CustomerMaster.this, HomeScreen.class);
		startActivity(_intent);
	}

	@Override
	public void onBackPressed() {
		GoBack();
	}

	public int convertDpToPixel(float dp) {
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return (int) px;
	}

	public void bindData() {
		customerSwipeListAdapter = new CustomerSwipeListAdapter(
				CustomerMaster.this, lstCustomers);
		customerSwipeListAdapter.notifyDataSetChanged();
		lvCustomer.setAdapter(customerSwipeListAdapter);
	}

	private class CustomerTask extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(CustomerMaster.this);

		protected void onPreExecute() {
			this.dialog.setMessage("Loading...");
			this.dialog.setCanceledOnTouchOutside(false);
			this.dialog.show();

		}

		protected Void doInBackground(final String... Params) {
			try {
				lstCustomers = ObjDBHelper.GetAllCustomers(false);

			} catch (Exception ex) {
				ex.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(final Void unused) {
			if (this.dialog.isShowing()) {
				this.dialog.dismiss();
				this.dialog = null;
			}
			try {
				bindData();
			} catch (Exception ex) {

				ex.printStackTrace();

			}

		}

	}
}